package com.freewater.springboot.demo.aop.api.advices.after;

import com.freewater.springboot.demo.aop.api.advices.CalculateImpl;
import com.freewater.springboot.demo.aop.api.advices.CalculateService;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.framework.adapter.GlobalAdvisorAdapterRegistry;

public class Main {

    public static void main(String[] args) {
        // 注册Advice适配器（把Advice转换成MethodInterceptor）
        GlobalAdvisorAdapterRegistry.getInstance().registerAdvisorAdapter(new AfterFinalAdviceAdaptor());
        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.addAdvice(new SimpleAfterAdvice());
        proxyFactory.setTarget(new CalculateImpl());

        final CalculateService proxy = (CalculateService) proxyFactory.getProxy();
        try {
            proxy.divide(10, 0);
        } catch (ArithmeticException e) {
            System.err.println("error: " + e.getMessage());
        }
    }
}
